package com.bite.bite_forum.services.Impl;
import com.bite.bite_forum.common.AppResult;
import com.bite.bite_forum.common.ResultCode;
import com.bite.bite_forum.dao.ArticleReplyMapper;
import com.bite.bite_forum.exception.ApplicationException;
import com.bite.bite_forum.model.Article;
import com.bite.bite_forum.model.ArticleReply;
import com.bite.bite_forum.services.IArticleReplyService;
import com.bite.bite_forum.services.IArticleService;
import com.bite.bite_forum.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {
    @Resource
    ArticleReplyMapper articleReplyMapper;
    @Resource
    IArticleService articleService;
    @Override
    public void create(ArticleReply articleReply) {
        //校验参数
        if(articleReply.getPostUserId() == null || articleReply.getArticleId() == null
                || StringUtils.isEmpty(articleReply.getContent())){
             log.info(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
             throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //帖子状态
        Article article = articleService.selectById(articleReply.getArticleId());
        if(article == null || article.getDeleteState() == 1){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        if(article.getState() == 1){
            log.info(ResultCode.FAILED_ARTICLE_BANNED.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
        //设置默认值 添加数据
        articleReply.setReplyId(0L);
        articleReply.setReplyUserId(0L);
        articleReply.setLikeCount(0);
        articleReply.setState((byte) 0);
        articleReply.setDeleteState((byte) 0);
        articleReply.setCreateTime(new Date());
        articleReply.setUpdateTime(new Date());
        int row = articleReplyMapper.insertSelective(articleReply);
        if(row != 1){
            log.info(ResultCode.ERROR_SERVICES.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //更新回复数
        articleService.addArticleReplyCountById(articleReply.getArticleId());
    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        Article article = articleService.selectById(articleId);
        if(article == null || article.getDeleteState() == 1){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        if(article.getState() == 1){
            log.info(ResultCode.FAILED_ARTICLE_BANNED.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
        return articleReplyMapper.selectByArticleId(articleId);
    }
}
